/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servelt;

import DatabaseAccessing.ProductModelController;
import DatabaseAccessing.Productcategory;
import DatabaseAccessing.Products;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author muhammad
 */
public class EditProduct extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        ProductModelController controller = new ProductModelController();

        if (request.getParameter("productID") != null &&
                request.getParameter("action") != null
                && request.getParameter("action").equals("get")) {

            Products product = controller.viewProductByID(Integer.parseInt(request.getParameter("productID")));

            List<Productcategory> categories = controller.viewAllCategorys();
            RequestDispatcher rd = request.getRequestDispatcher("EditProduct.jsp");
            request.setAttribute("allCategories", categories);
            request.setAttribute("product", product);
            rd.forward(request, response);

        } else if (request.getParameter("productID") != null
                && request.getParameter("action") != null
                && request.getParameter("action").equals("save")) {

            try {
                String productName = request.getParameter("productName");
                float productPrice = Float.parseFloat(request.getParameter("productPrice"));
                int categoryID = Integer.parseInt(request.getParameter("lstCategories"));
                int productAmount = Integer.parseInt(request.getParameter("productAmount"));
                String productDescription = request.getParameter("productDescription");

                Products product = controller.viewProductByID(Integer.parseInt(request.getParameter("productID")));
                product.setProductName(productName);
                product.setPrice(productPrice);
                product.setAmount(productAmount);
                product.setProductNotes(productDescription);
                product.setProductCategoryId(categoryID);

                controller.editProduct(product);

                RequestDispatcher rd = request.getRequestDispatcher("AddProductDone.jsp");
                request.setAttribute("message", "Product Saved successfully.");
                rd.forward(request, response);
            } catch (Exception ex) {
                RequestDispatcher rd = request.getRequestDispatcher("AddProductDone.jsp");
                request.setAttribute("message", "Error: Product Couldn't be saved.");
                rd.forward(request, response);
            }

        }

    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
